<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入客户年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="客户微信" prop="wechat">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.wechat"-->
      <!--          placeholder="请输入潜在客户微信"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="潜在客户qq" prop="qq">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.qq"-->
      <!--          placeholder="请输入潜在客户qq"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入客户联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="潜在客户工作年限" prop="workYears">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workYears"-->
      <!--          placeholder="请输入潜在客户工作年限"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="毕业时间" prop="graduationTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.graduationTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择毕业时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="来源：他人推荐、公众号" prop="channel">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.channel"-->
      <!--          placeholder="请输入来源：他人推荐、公众号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="推荐人" prop="referrers">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.referrers"-->
      <!--          placeholder="请输入推荐人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="意向学科" prop="degreeDiscipline">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.degreeDiscipline"-->
      <!--          placeholder="请输入意向学科"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="意向班级" prop="degreeClasses">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.degreeClasses"-->
      <!--          placeholder="请输入意向班级"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="录入人" prop="entrantTeacher">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.entrantTeacher"-->
      <!--          placeholder="请输入录入人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="咨询老师" prop="consultTeacher">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.consultTeacher"-->
      <!--          placeholder="请输入咨询老师"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="录入时间" prop="entryTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.entryTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择录入时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="最后追踪时间" prop="astTraceDate">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.astTraceDate"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择最后追踪时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="下次追踪时间" prop="nextTraceTime">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.nextTraceTime"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择下次追踪时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prospects:prospects:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['prospects:prospects:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['prospects:prospects:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['prospects:prospects:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-s-custom"
          size="mini"
          :disabled="single"
          @click="toFullTraineesHandle"
        >转为正式学员
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-s-custom"
          size="mini"
          :disabled="single"
          @click="followUpHandle"
        >跟进
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prospectsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <!-- 姓名列 -->
      <el-table-column label="姓名" align="center" prop="name">
        <template slot-scope="scope">
          <div @click="drawer = true" >
            <el-button @click="showClienMsg(scope.row)" plain style="border: none">
              {{ scope.row.name }}
            </el-button>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="微信" align="center" width="100" prop="wechat"/>
      <el-table-column label="qq" align="center" prop="qq"/>
      <el-table-column label="联系电话" align="center" width="120" prop="phone"/>
      <el-table-column label="性别" align="center" prop="sex" :formatter="formatSex"/>
      <el-table-column label="工作年限" align="center" prop="workYears"/>
      <el-table-column label="毕业时间" align="center" prop="graduationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.graduationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="渠道" align="center" width="120" prop="channel"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="formatStatus"/>
      <el-table-column label="学员推荐" align="center" prop="isOldCadetsRecom" :formatter="formatIsOldCadetsRecom"/>
      <el-table-column label="推荐人" align="center" prop="referrers"/>
      <el-table-column label="意向程度" align="center" prop="degreeIntent" :formatter="formatDegreeIntent"/>
      <el-table-column label="意向学科" align="center" prop="degreeDiscipline"/>
      <el-table-column label="意向班级" align="center" prop="degreeClasses"/>
      <el-table-column label="录入人" align="center" prop="entrantTeacher" :formatter="formatEntrantTeacher"/>
      <el-table-column label="咨询老师" align="center" prop="consultTeacher"/>
      <el-table-column label="录入时间" align="center" prop="entryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后追踪时间" align="center" prop="astTraceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.astTraceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下次追踪时间" align="center" prop="nextTraceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextTraceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['prospects:prospects:edit']">修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['prospects:prospects:remove']">删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-promotion"
            @click="turnOver(scope.row)">移交
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>



    <el-drawer
      :title="form.name"
      :visible.sync="drawer">
      <template>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="基本信息" name="first">
            <el-descriptions class="margin-top"  :column="2"  style="padding-left: 20px" >
              <el-descriptions-item label="建档时间">{{form.entryTime}}</el-descriptions-item>
              <el-descriptions-item label="录入人">{{formatPerson(form.entrantTeacher)}}</el-descriptions-item>
              <el-descriptions-item label="用户名">{{form.name}}</el-descriptions-item>
              <el-descriptions-item label="手机号">{{form.phone}}</el-descriptions-item>
              <el-descriptions-item label="性别" >{{form.sex == 0 ? "男":"女"}}</el-descriptions-item>
              <el-descriptions-item label="来源">{{form.channel}}</el-descriptions-item>
              <el-descriptions-item label="当前状态">{{drwaerFromatStatus(form.status)}}</el-descriptions-item>
              <el-descriptions-item label="QQ">{{form.qq}}</el-descriptions-item>
              <el-descriptions-item label="微信">{{form.wechat}}</el-descriptions-item>
              <el-descriptions-item label="毕业时间">{{form.graduationTime}}</el-descriptions-item>
              <el-descriptions-item label="工作时间">{{form.workYears}}年</el-descriptions-item>
              <el-descriptions-item label="是否老学员推荐">{{form.isOldCadetsRecom == 0 ? "否": "是"}}</el-descriptions-item>
              <el-descriptions-item v-if="form.isOldCadetsRecom == 1" label="介绍人">{{form.referrers}}</el-descriptions-item>
              <el-descriptions-item label="意向程度">{{form.degreeIntent == 0 ? "弱": "强"}}</el-descriptions-item>
              <el-descriptions-item label="意向班级">{{form.degreeClasses}}</el-descriptions-item>
              <el-descriptions-item label="意向学科">{{form.degreeDiscipline}}</el-descriptions-item>
              <el-descriptions-item label="备注">{{form.remark}}</el-descriptions-item>
            </el-descriptions>
          </el-tab-pane>
          <el-tab-pane label="跟踪记录" name="second">
              <el-table v-loading="loading" :data="currentStudentLog">
                <el-table-column label="编号" align="center" prop="id"/>
                <el-table-column label="跟进学员" align="center" prop="followUpStudent"/>
                <el-table-column label="跟进日期" align="center" prop="followUpDate"/>
                <el-table-column label="跟进方式" align="center" prop="followUpWay" :formatter="wayFormat"/>
                <el-table-column label="备注" align="center" prop="remark"/>
              </el-table>
          </el-tab-pane>
          <el-tab-pane label="潜在学院动态" name="third">潜在学院动态</el-tab-pane>
        </el-tabs>
      </template>
    </el-drawer>



    <!-- 添加或修改潜在客户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入潜在姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" placeholder="请输入潜在客户年龄"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="微信" prop="wechat">
              <el-input v-model="form.wechat" placeholder="请输入潜在客户微信"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="qq" prop="qq">
              <el-input v-model="form.qq" placeholder="请输入潜在客户qq"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入潜在客户联系电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作年限" prop="workYears">
              <el-input v-model="form.workYears" placeholder="请输入潜在客户工作年限"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="咨询老师" prop="consultTeacher">
              <el-input v-model="form.consultTeacher" placeholder="请输入咨询老师"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来源" prop="channel">
              <el-input v-model="form.channel" placeholder="他人推荐、公众号等渠道"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="推荐人" prop="referrers">
              <el-input v-model="form.referrers" placeholder="请输入推荐人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="意向学科" prop="degreeDiscipline">
              <el-input v-model="form.degreeDiscipline" placeholder="请输入意向学科"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="意向班级" prop="degreeClasses">
              <el-input v-model="form.degreeClasses" placeholder="请输入意向班级"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="录入人" prop="entrantTeacher">
              <el-input v-model="form.entrantTeacher" placeholder="请输入录入人"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="毕业时间" prop="graduationTime">
              <el-date-picker clearable
                              v-model="form.graduationTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择毕业时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="录入时间" prop="entryTime">
              <el-date-picker clearable
                              v-model="form.entryTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择录入时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="最后追踪时间" prop="astTraceDate">
              <el-date-picker clearable
                              v-model="form.astTraceDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择最后追踪时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下次追踪时间" prop="nextTraceTime">
              <el-date-picker clearable
                              v-model="form.nextTraceTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择下次追踪时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 移交客户对话框 -->
    <el-dialog :title="title" :visible.sync="turnOverOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        <el-form-item label="移交时间" prop="turnOverTime">-->
        <!--          <el-date-picker clearable-->
        <!--                          v-model="form.turnOverTime"-->
        <!--                          type="date"-->
        <!--                          value-format="yyyy-MM-dd"-->
        <!--                          placeholder="请选择移交时间">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="移交人" prop="trunOverPerson">-->
        <!--          <el-input v-model="form.trunOverPerson" placeholder="请输入移交人" />-->
        <!--        </el-form-item>-->
        <el-form-item label="接收人" prop="receiver">
          <el-input v-model="turnOverParams.receiver" placeholder="请输入接收人"/>
        </el-form-item>
        <el-form-item label="被移交人" prop="transferee">
          <el-input v-model="form.name" placeholder="请输入被移交人" readonly/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="turnOverSubmitForm">确 定</el-button>
        <el-button @click="turnOverCancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 跟进客户对话框 -->
    <el-dialog :title="title" :visible.sync="followUpOpen" width="500px" append-to-body>
      <el-form ref="form" :model="followUp" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="跟进学员" prop="followUpStudent">
              <el-input v-model="followUp.followUpStudent" placeholder="请输入潜在姓名" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="followUp.phone" placeholder="请输入潜在客户联系电话" readonly/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="qq" prop="qq">
              <el-input v-model="followUp.qq" placeholder="请输入潜在客户qq" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信" prop="wechat">
              <el-input v-model="followUp.wechat" placeholder="请输入潜在客户微信" readonly/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="意向程度">
              <el-select v-model="followUp.degreeIntent" placeholder="请选择意向程度">
                <el-option
                  v-for="item in degreeIntentList"
                  :key="item.id"
                  :label="item.degreeIntent"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="跟进方式">
              <el-select v-model="followUp.followUpWay" placeholder="请选择跟进方式">
                <el-option
                  v-for="item in followUpWayList"
                  :key="item.id"
                  :label="item.followUpWay"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="下次追踪时间" prop="nextTraceTime">
          <el-date-picker clearable
                          v-model="followUp.nextTraceTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择下次追踪时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="followUp.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="followUpSubmitForm">确 定</el-button>
        <el-button @click="followUpCancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listProspects,
  getProspects,
  delProspects,
  addProspects,
  updateProspects,
  turnOverClient,
  toFullTrainees,
  followUpLog
} from "@/api/prospects/prospects";
import {listUser} from "@/api/system/user";
import item from "@/layout/components/Sidebar/Item.vue";
import {listWay} from "@/api/system/way";
import { listLog } from "@/api/system/log";

export default {
  name: "Prospects",
  data() {
    return {
      //跟进记录表
      logList:[],
      //当前学员的跟进记录
      currentStudentLog:[],
      //选项卡
      activeName: 'first',
      //抽屉的开启
      drawer: false,
      //跟进方式
      followUpWayList: [],
      //意向程度
      degreeIntentList: [
        {
          id: 0,
          degreeIntent: "弱"
        },
        {
          id: 1,
          degreeIntent: "强"
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组设置name
      name: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 潜在客户表格数据
      prospectsList: [],
      //用户列表
      userList: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示移交对话弹出层
      turnOverOpen: false,
      // 是否显示跟进客户对话框
      followUpOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        age: null,
        wechat: null,
        qq: null,
        phone: null,
        sex: null,
        workYears: null,
        graduationTime: null,
        channel: null,
        status: null,
        isOldCadetsRecom: null,
        referrers: null,
        degreeIntent: null,
        degreeDiscipline: null,
        degreeClasses: null,
        entrantTeacher: null,
        consultTeacher: null,
        entryTime: null,
        astTraceDate: null,
        nextTraceTime: null,
      },
      // 表单参数
      form: {},
      // 跟进参数
      followUp: {
        followUpStudentId: undefined,
        followUpStudent: undefined,
        phone: undefined,
        qq: undefined,
        wechat: undefined,
        degreeIntent: undefined,
        degreeClasses: undefined,
        followUpWay: undefined,
        nextTraceTime: undefined,
        remark: undefined,
      },
      // 表单校验
      rules: {},
      //移交参数
      turnOverParams: {
        id: null,
        receiver: null
      },
    };
  },
  created() {
    this.getList();
    this.getUserList();
    this.getFollowUpWayList();
    this.getLogList();
  },
  methods: {
    /*选项卡切换*/
    handleClick(tab, event) {
      console.log(tab, event);
    },
    /*学员推荐格式化 */
    formatIsOldCadetsRecom(cellValue) {
      return cellValue.isOldCadetsRecom == 1 ? "是" : "否";
    },
    /*学员状态格式化*/
    formatStatus(cellValue) {
      const statusMap = {
        0: "有意向",
        1: "已经报名",
        2: "无意向"
      };
      return statusMap[cellValue.status] || "未知";
    },
    /** 性别格式化 */
    formatSex(cellValue) {
      return cellValue.sex == 0 ? "男" : "女";
    },
    /**意向程度格式化 */
    formatDegreeIntent(cellValue) {
      return cellValue.degreeIntent == 0 ? "弱" : "强";
    },
    /** 录入人格式化 */
    formatEntrantTeacher(cellValue) {
      return this.userList[cellValue.entrantTeacher] || "未知";
    },
    /** 跟进方式格式化*/
    wayFormat(cellValue){
      const followUpWayObject = this.followUpWayList.reduce((acc, item) => {
        acc[item.id] = item.followUpWay;
        return acc;
      }, {});
      return followUpWayObject[cellValue.id]
    },

    /** 抽屉 录入人格式化 */
    formatPerson(person){
      return this.userList[person] || "未知";
    },
    /** 抽屉 状态格式化 */
    drwaerFromatStatus(status){
      const statusMap = {
        0: "有意向",
        1: "已经报名",
        2: "无意向"
      };
      return statusMap[status] || "未知"
    },

    /** 转为正式学员按钮 */
    toFullTraineesHandle() {
      const id = this.ids[0];
      const name = this.name[0];

      this.$modal.confirm('是否确认客户:' + name + '已经缴费，将其转为正式学员').then(() => {
        // 直接调用 toFullTrainees
        toFullTrainees(id)
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("操作成功");
          })
          .catch((error) => {
            console.log(error)
          });
      }).catch((error) => {
        console.log(error)
      });
    },
    /** 跟进学员按钮 */
    followUpHandle() {
      this.reset();
      const id = this.ids
      getProspects(id).then(response => {
        let {
          followUpStudentId,
          followUpStudent,
          phone,
          qq,
          wechat,
          degreeIntent,
          degreeClasses,
          followUpWay,
          nextTraceTime,
          remark
        } = response.data;
        this.followUp = {
          followUpStudentId,
          followUpStudent,
          phone,
          qq,
          wechat,
          degreeIntent,
          degreeClasses,
          followUpWay,
          nextTraceTime,
          remark
        };
        this.followUp.followUpStudentId = response.data.id;
        this.followUp.followUpStudent = response.data.name;
        this.followUpOpen = true;
        this.title = "跟进潜在客户";
      });
    },
    /** 跟进学员按钮确认 */
    followUpSubmitForm() {
      for (let followUpKey in this.followUp) {
        if (this.followUp[followUpKey] === undefined || this.followUp[followUpKey] === "") {
          this.$modal.msgWarning("请将信息补充完整后提交");
          return;
        }
      }
      console.log(this.followUp)
      followUpLog(this.followUp).then(response => {
        this.$modal.msgSuccess("跟进成功");
        this.followUpOpen = false;
        this.getList();
      }).catch(error => {
        this.$modal.msgError("跟进失败");
        this.followUpOpen = false;
      })
    },
    /** 跟进学员按钮取消 */
    followUpCancel() {
      this.followUpOpen = false;
      this.resetFollowUp();
    },
    /** 抽屉的弹出事件*/
    showClienMsg(row){
      this.drawer = true;
      console.log(row);
      this.form = row;
      debugger;
      this.currentStudentLog = this.logList.filter(log => {
       return log.followUpStudent === row.name
      });
    },
    /*重置跟进表单信息*/
    resetFollowUp() {
      this.followUp = {
        id: undefined,
        name: undefined,
        phone: undefined,
        qq: undefined,
        wechat: undefined,
        degreeIntent: undefined,
        degreeClasses: undefined,
        followUpWay: undefined,
        nextTraceTime: undefined,
        remark: undefined,
      };
    },

    /** 查询潜在客户跟进记录列表 */
    getLogList() {
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
      });
    },
    /** 查询潜在客户跟进方式列表 */
    getFollowUpWayList() {
      listWay(this.queryParams).then(response => {
        this.followUpWayList = response.rows;
      });
    },
    /** 查询潜在客户列表 */
    getList() {
      this.loading = true;
      listProspects(this.queryParams).then(response => {
        this.prospectsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询用户列表 */
    getUserList() {
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows.reduce((result, user) => {
            result[user.userId] = user.userName;
            return result;
          }, {});
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        age: null,
        wechat: null,
        qq: null,
        phone: null,
        sex: null,
        workYears: null,
        graduationTime: null,
        channel: null,
        status: null,
        isOldCadetsRecom: null,
        referrers: null,
        degreeIntent: null,
        degreeDiscipline: null,
        degreeClasses: null,
        entrantTeacher: null,
        consultTeacher: null,
        entryTime: null,
        astTraceDate: null,
        nextTraceTime: null,
        remark: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.name = selection.map(item => item.name)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 移交按钮操作 */
    turnOver(row) {
      this.reset();
      const id = row.id;
      getProspects(id).then(response => {
        this.form = response.data;
        this.turnOverParams.id = row.id;
        this.turnOverOpen = true;
        this.title = "移交潜在客户"
      });
    },
    /** 移交确认按钮 */
    turnOverSubmitForm() {
      turnOverClient(this.turnOverParams).then(response => {
        this.$modal.msgSuccess("移交成功");
        this.turnOverOpen = false;
        this.getList();
      }).catch(error => {
        this.$modal.msgError("移交失败");
        this.turnOverOpen = false;
      })
    },
    /** 移交取消按钮 */
    turnOverCancel() {
      this.turnOverOpen = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加潜在客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProspects(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改潜在客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProspects(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProspects(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除潜在客户编号为"' + ids + '"的数据项？').then(function () {
        return delProspects(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('prospects/prospects/export', {
        ...this.queryParams
      }, `prospects_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
